const express = require('express')
const utils = require('utility')
const Router = express.Router()
const model = require('./model')
const User = model.getModel('user')

Router.get('/list',function(req,res){

	User.find({},function(err,doc){
		return res.json(doc)
	})
})

Router.post('/register',function(req,res){
	console.log(req.body)
	const {user,pwd,type}=req.body
	User.findOne({user:user},function(err,doc){ //es6语法 {user:user}key和value可以简写{user}
		if(doc){
			return res.json({code:1,msg:'用户名重复'})
		}
		User.create({user,pwd:_md5(pwd),type},function(e,d){
			if(e){
				return res.json({code:1,msg:'后端出错了'})
			}
			return res.json({code:0})
		})


	})


})

function _md5(pwd){

	let salt = '@#$%*&erjerjgl~erferewrw'

	return utils.md5( utils.md5(pwd+salt) )
}



Router.get('/info',function(req, res){
	// 用户有没有cookie
	return res.json({code:0})
})

module.exports = Router